package com.asion.gankapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asion.gankapp.R;
import com.asion.gankapp.entrys.BaseEntry;
import com.asion.gankapp.entrys.BaseMeituEntry;
import com.asion.gankapp.servers.Api;
import com.asion.gankapp.utils.SlideLayout;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;

import gl.com.app.Gank;


/**
 * Created by asion on 16-10-5.
 */

public class BaseRecyclerAdapter extends RecyclerView.Adapter<BaseRecyclerAdapter.MyHolder> {

    public static final int ALLTYPE = 1;
    public static final int MEITU = 2;
    public static final int TYPE_DOWNLOAD = 3;

    private Context context;
    private int vType;
    private List<Object> list;
    private OnItemClickListener onItemClickListener;
    private List<SlideLayout> slideDeleteArrayList = new ArrayList<>();

    public void setOnIntenetClickListenr(OnIntenetClickListenr onIntenetClickListenr) {
        this.onIntenetClickListenr = onIntenetClickListenr;
    }

    private OnIntenetClickListenr onIntenetClickListenr;

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        this.onImageClickListener = onImageClickListener;
    }

    private OnImageClickListener onImageClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private OnDeleteClickListener onDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setType(int type) {
        this.vType = type;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (vType == BaseRecyclerAdapter.ALLTYPE) {
            view = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
        }
        if (vType == MEITU) {
            view = LayoutInflater.from(context).inflate(R.layout.meitu_item, parent, false);
        }
        if (vType == TYPE_DOWNLOAD) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.download_item, parent, false);
        }
        return view == null ? null : new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        Object t = list.get(position);
        if (t instanceof BaseEntry.ResultsBean) {
            BaseEntry.ResultsBean bean = (BaseEntry.ResultsBean) t;
            holder.baseDesc.setText(bean.getDesc());
            holder.baseType.setText(bean.getType());
            holder.baseWho.setText(bean.getWho());
            holder.baseDate.setText(bean.getPublishedAt());
            holder.itemView.setOnClickListener(v ->
                    onItemClickListener.onItemClick(holder.itemView, position, bean, null));
        } else if (t instanceof BaseMeituEntry.TngouBean) {
            String string = Api.image + ((BaseMeituEntry.TngouBean) t).getImg();
            Glide.with(context).load(string).asBitmap()
                    .placeholder(R.drawable.loading)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .dontAnimate()
                    .centerCrop()
                    .override(1000, 1200)
                    .error(R.drawable.error)
                    .into(holder.meitu);
            holder.meitu.setOnClickListener(v -> onImageClickListener.onImageClick(holder.meitu, position, string));
            holder.intenet.setOnClickListener(v -> onIntenetClickListenr.onIntenetCLick(holder.itemView, ((BaseMeituEntry.TngouBean) t).getId(), ((BaseMeituEntry.TngouBean) t).getTitle()));
        } else if (t instanceof File) {
            Glide.with(context).load(((File) t).getAbsoluteFile()).asBitmap().override(1000, 1200).into(holder.benefils_meitu);
            holder.delete.setOnClickListener((v) -> onDeleteClickListener.onDeleteClick(holder.benefils_meitu, position, ((File) t).getName()));
            holder.slidelayout.setOnSlideDeleteListener(new SlideLayout.OnSlideDeleteListener() {
                @Override
                public void onOpen(SlideLayout slideDelete) {
                    closeOtherItem();
                    slideDeleteArrayList.add(slideDelete);
                }

                @Override
                public void onClose(SlideLayout slideDelete) {
                    slideDeleteArrayList.remove(slideDelete);
                }
            });
        } else if (t instanceof Gank) {
            Gank gank = (Gank) t;
            holder.baseDesc.setText(gank.getDesc());
            holder.baseType.setText(gank.getType());
            holder.baseWho.setText(gank.getWho());
            holder.baseDate.setText(gank.getPublishedAt());
            holder.itemView.setOnClickListener(v ->
                    onItemClickListener.onItemClick(holder.itemView, position, null, gank));
        }
    }

    private void closeOtherItem() {
        ListIterator<SlideLayout> slideDeleteListIterator = slideDeleteArrayList.listIterator();
        while (slideDeleteListIterator.hasNext()) {
            SlideLayout slideDelete = slideDeleteListIterator.next();
            slideDelete.isShowDelete(false);
        }
        slideDeleteArrayList.clear();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {

        private ImageButton intenet;
        private ImageView meitu;
        private TextView baseDesc;
        private TextView baseType;
        private TextView baseWho;
        private TextView baseDate;
        private SlideLayout slidelayout;
        private RelativeLayout delete;
        private ImageView benefils_meitu;

        public MyHolder(View itemView) {
            super(itemView);
            baseDesc = ((TextView) itemView.findViewById(R.id.base_desc));
            baseType = ((TextView) itemView.findViewById(R.id.base_type));
            baseWho = ((TextView) itemView.findViewById(R.id.base_who));
            baseDate = ((TextView) itemView.findViewById(R.id.base_date));

            meitu = ((ImageView) itemView.findViewById(R.id.meitu_image));
            intenet = ((ImageButton) itemView.findViewById(R.id.meitu_intenet));

            delete = (RelativeLayout) itemView.findViewById(R.id.delete);
            slidelayout = ((SlideLayout) itemView.findViewById(R.id.slideLayout));
            benefils_meitu = (ImageView) itemView.findViewById(R.id.benefils_meitu);
        }
    }

    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        list.remove(position);
//        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public void removeAll() {
        list.clear();
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, BaseEntry.ResultsBean resultsBean, Gank gank);
    }

    public interface OnImageClickListener {
        void onImageClick(View view, int position, String url);
    }

    public interface OnIntenetClickListenr {
        void onIntenetCLick(View view, int id, String title);
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(View view, int position, String name);
    }
}
